﻿using LetsTestAPI.EDM;
using LetsTestAPI.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace LetsTestAPI.Controllers
{
    public class MWTTESTINSTANCEController : ApiController
    {

        public string GetListOfTestInstances()
        {
            using (LetsTestEntities context = new LetsTestEntities())
            {
                List<TestInstanceModel> models = new List<TestInstanceModel>();
                List<Instance> instances = context.Instances.Where(t => t.IsActive == true).ToList();
                foreach (Instance i in instances)
                {
                    models.Add(new TestInstanceModel(i.CandidateID, i.CreatorID, i.TestID));
                }
                JArray job = JArray.FromObject(models);
                return job.ToString();
            }
        }

        public string GetTestInstance(int InstanceID)
        {
            using (LetsTestEntities context = new LetsTestEntities())
            {
                Instance i = context.Instances.Where(t => t.Id == InstanceID).FirstOrDefault();
                TestInstanceModel model = new TestInstanceModel(i.CandidateID, i.CreatorID, i.TestID);
                JObject job = JObject.FromObject(model);
                return job.ToString();
            }
        }

        [HttpPost]
        public void AddOrEditTestInstance([FromBody]JObject jsonData)
        {
            //string Name = String.Empty;
            int TestID = 0;
            int SpecialistID = 0;
            //int Time = 0;
            string IsActive = String.Empty;
            //if (jsonData["Name"] != null) { Name = jsonData["Name"].ToString(); }
            if (jsonData["TestID"] != null) TestID = Int32.Parse(jsonData["TestID"].ToString());
            if (jsonData["SpecialistID"] != null) SpecialistID = Int32.Parse(jsonData["SpecialistID"].ToString());
            //if (jsonData["Time"] != null) Time = Int32.Parse(jsonData["Time"].ToString());
            if (jsonData["IsActive"] != null) IsActive = jsonData["IsActive"].ToString();

            using (LetsTestEntities context = new LetsTestEntities())
            {
                if (TestID != 0)
                {
                    var rows = context.Instances.Where(t => t.Id == TestID);
                    if (rows != null)
                    {
                        var row = rows.FirstOrDefault();
                        //if (SpecialistID != 0) row.SpecialistID = SpecialistID;
                        if (!String.IsNullOrEmpty(IsActive)) row.IsActive = Boolean.Parse(IsActive);
                        context.SaveChanges();
                    }
                }
                else
                {
                    context.Instances.Add(new Instance { CreatorID = 1,  CandidateID = 1, IsActive = true });
                    context.SaveChanges();
                }
            }


        }
    }
}
